import java.util.Comparator;
import java.util.PriorityQueue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 刘国水
 * Date: 2025-05-14
 * Time: 21:32
 */
public class test {
    public int[] smallestK(int[] arr, int k) {
        int[] ret = new int[k];
        if(arr == null || k == 0) {
            return ret;
        }

        PriorityQueue<Integer> maxHeel = new PriorityQueue<>(k, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2.compareTo(o1);
            }
        });

        for (int i = 0; i < k; i++) {
            maxHeel.offer(arr[i]);
        }

        for (int i = k; i < arr.length ; i++) {
            int peekVal = maxHeel.peek();
            if(peekVal > arr[i]) {
                maxHeel.poll();
                maxHeel.offer(arr[i]);
            }
        }
        for (int i = 0; i < k; i++) {
            int val = maxHeel.poll();
            ret[i] = val;
        }
        return ret;

    }
//    public int[] smallestK(int[] arr, int k) {
////        int[] ret = new int[k];
////        if(arr == null || k == 0) {
////            return ret;
////        }
////        PriorityQueue<Integer> hai = new PriorityQueue<>();
////
////        for (int i = 0; i < arr.length; i++) {
////            hai.offer(arr[i]);
////        }
////
////        for (int i = 0; i < k; i++) {
////            int val = hai.poll();
////            ret[i] = val;
////        }
////        return ret;
//    }

//    public int[] smallestK(int[] arr, int k) {
//        int[] ret = new int[k];
//        if(arr == null) {
//            return ret;
//        }
//
//        PriorityQueue<Integer> array = new PriorityQueue<>();
//
//        for (int i = 0; i < arr.length; i++) {
//            array.offer(arr[i]);
//        }
//
//        for (int i = 0; i < k; i++) {
//            int val = array.poll();
//            ret[i] = val;
//        }
//
//    }
}

